<?php

/*
	info: 获取地址列表
*/

define('IS_MEMBER_CHECK', TRUE);
require '../common.inc.php';

global_CheckPost(
	array(
		array('order','str','','',0,''),
		array('perpage','int','@>0','',0,5),
		array('page','int','@>0','',0,1),
));

$webglobal['CPP']['id'] = api_GetTopActivityID();
$webglobal['codeinfo'] = $db->GetOne("SELECT * FROM promotion_user WHERE pid='{$webglobal['CPP']['id']}' AND uid='{$user['m_uid']}'");
if (empty($webglobal['codeinfo']))
{
	api_EchoReturn($weblang['api_codeError']);
}

$ordersql = ' ORDER BY ';
switch ($webglobal['CPP']['order'])
{
	case 'vacancies':
		$ordersql .= 'havepeoplenum DESC';
		break;
	case 'dates':
		$ordersql .= 'begintime ASC';
		break;
	default:
		$ordersql .= 'havepeoplenum DESC';
}

$return['data']['sitetotal'] = 0;
$return['data']['sites'] = array();

$return['data']['sitetotal'] = $db->GetNum("SELECT COUNT(*) FROM promotion_site WHERE pid='{$webglobal['CPP']['id']}' AND begintime>'{$webserver['timestamp']}'");
if ($return['data']['sitetotal'] > 0)
{
	$tempstartnum = ($webglobal['CPP']['page']-1)*$webglobal['CPP']['perpage'];
	$limitsql = ($webglobal['CPP']['perpage']>0) ? " LIMIT {$tempstartnum}, {$webglobal['CPP']['perpage']}" : "";
	$query = $db->Query("SELECT *,(maxpeoplenum-curpeoplenum) AS havepeoplenum FROM promotion_site WHERE pid='{$webglobal['CPP']['id']}' AND begintime>'{$webserver['timestamp']}'{$ordersql}{$limitsql}");
	while ($tempdata = $db->FetchArray($query))
	{
		$tempalbums = array();
		$query1 = $db->Query("SELECT * FROM promotion_site_img WHERE sid='{$tempdata['id']}' ORDER BY imgorder ASC,id ASC");
		while ($tempdata1 = $db->FetchArray($query1))
		{
			$tempalbums[] = array(
				'id' => $tempdata1['id'],
				'imginfo' => $tempdata1['imginfo'],
				'imgfile' => $webconfig['webimageurl'].$tempdata1['imgfile'],
			);
		}
		$return['data']['sites'][] = array(
			'siteid' => $tempdata['id'],
			'sitename' => $tempdata['sitename'],
			'begintime' => !empty($tempdata['begintime']) ? global_FormatTime($tempdata['begintime'], $webglobal['sitetimemode']) : '',
			'begintime_num' => $tempdata['begintime'],
			'city' => $tempdata['city'],
			'address' => $tempdata['address'],
			'telephone' => $tempdata['telephone'],
			'info' => $tempdata['info'],
			'coverimg' => !empty($tempdata['coverimg']) ? $webconfig['webimageurl'].$tempdata['coverimg'] : '',
			'latitude' => $tempdata['latitude'],
			'longitude' => $tempdata['longitude'],
			'maxpeoplenum' => $tempdata['maxpeoplenum'],
			'curpeoplenum' => $tempdata['curpeoplenum'],
			'albums' => $tempalbums,
		);
	}
}

$return['code'] = 0;
api_EchoReturn();
